package icu.qx.pbs.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysUser;
import icu.qx.pbs.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pbs.domain.PbsWorkGroup;
import icu.qx.pbs.service.IPbsWorkGroupService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产班组Controller
 *
 * @author yong
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/pbs/workGroup")
public class PbsWorkGroupController extends BaseController {
    @Autowired
    private IPbsWorkGroupService pbsWorkGroupService;

    @Autowired
    private SysUserFeign sysUserFeign;

    /**
     * 查询生产班组列表
     */
    @RequiresPermissions("pbs:workGroup:list")
    @GetMapping("/list")
    public TableDataInfo list(PbsWorkGroup pbsWorkGroup) {
        startPage();
        List<PbsWorkGroup> list = pbsWorkGroupService.selectPbsWorkGroupList(pbsWorkGroup);
        list.forEach(e -> {
            R<SysUser> userR = sysUserFeign.username(e.getWgManager());
            String userName = userR.getData().getUserName();
            e.setWgManagerName(userName);
        });
        return getDataTable(list);
    }

    /**
     * 导出生产班组列表
     */
    @RequiresPermissions("pbs:workGroup:export")
    @Log(title = "生产班组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PbsWorkGroup pbsWorkGroup) {
        List<PbsWorkGroup> list = pbsWorkGroupService.selectPbsWorkGroupList(pbsWorkGroup);
        ExcelUtil<PbsWorkGroup> util = new ExcelUtil<PbsWorkGroup>(PbsWorkGroup.class);
        util.exportExcel(response, list, "生产班组数据");
    }

    /**
     * 获取生产班组详细信息
     */
    @RequiresPermissions("pbs:workGroup:query")
    @GetMapping(value = "/{wgCode}")
    public AjaxResult getInfo(@PathVariable("wgCode") String wgCode) {
        PbsWorkGroup pbsWorkGroup = pbsWorkGroupService.selectPbsWorkGroupByWgCode(wgCode);
        // 查询负责人
        List<Object> users = new ArrayList<>();
        AjaxResult ajaxResult = AjaxResult.success(pbsWorkGroup);
        pbsWorkGroup.getPbsWorkGroupPeos().forEach(e -> {
            AjaxResult res = sysUserFeign.myUser(e.getWgroupPeo());
            users.add(res.get("data"));
        });
        ajaxResult.put("groupPeos", users);
        ajaxResult.put("manager", sysUserFeign.myUser(pbsWorkGroup.getWgManager()).get("data"));
        return ajaxResult;
    }

    /**
     * 新增生产班组
     */
    @RequiresPermissions("pbs:workGroup:add")
    @Log(title = "生产班组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PbsWorkGroup pbsWorkGroup) {
        return toAjax(pbsWorkGroupService.insertPbsWorkGroup(pbsWorkGroup));
    }

    /**
     * 修改生产班组
     */
    @RequiresPermissions("pbs:workGroup:edit")
    @Log(title = "生产班组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PbsWorkGroup pbsWorkGroup) {
        return toAjax(pbsWorkGroupService.updatePbsWorkGroup(pbsWorkGroup));
    }

    /**
     * 删除生产班组
     */
    @RequiresPermissions("pbs:workGroup:remove")
    @Log(title = "生产班组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wgCodes}")
    public AjaxResult remove(@PathVariable String[] wgCodes) {
        return toAjax(pbsWorkGroupService.deletePbsWorkGroupByWgCodes(wgCodes));
    }
}
